package buyAndSellOnLine;

import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.HashMap;

public class BuyAndSellOnLine {
	interface Agent extends Remote {
		void buy(int buyerCreditAccount, int productID) throws RemoteException;
	}

	class ProductDetails {
		int id;
		String name;
		String description;
		int price;
		Agent agent;
	}

	interface Catalog extends Remote {
		ProductDetails[] searchByName(String name) throws RemoteException;

		ProductDetails[] searchByDescription(String name)
				throws RemoteException;

		ProductDetails[] searchById(int id) throws RemoteException;

		int addToCatalog(ProductDetails[] products) throws RemoteException;

		void removeFromCatalog(ProductDetails[] products, int password)
				throws RemoteException;
	}

	public class Client implements Agent {
		private String serverAddress = "127.0.0.1";
		private int serverPort;
		private Remote server;

		public Client() {
			try {
				Registry registry = LocateRegistry.getRegistry(serverAddress,
						serverPort);
				server=registry.lookup("BuyAndSellOnLineServer");

			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		public void buy(int buyerCreditAccount, int productID)
				throws RemoteException {

		}

	}

	public class BuyAndSellOnLineServer implements Catalog {
		private static final String serverName = "BuyAndSellOnLine";
		private static final String address = "127.0.0.1";
		private static final int port = 1977;
		private Registry registry = null;
		HashMap<Integer, ProductDetails> map;

		public BuyAndSellOnLineServer() {
			try {
				if (registry == null)
					LocateRegistry.createRegistry(port);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			try {
				registry.rebind(serverName, this);
			} catch (AccessException e) {
				e.printStackTrace();
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			map = new HashMap<Integer, ProductDetails>();
		}

		public Remote getServer() {
			return this;
		}

		@Override
		public int addToCatalog(ProductDetails[] products)
				throws RemoteException {
			for (int i = 0; i < products.length; i++)
				map.put(products[i].id, products[i]);
			return 0;
		}

		@Override
		public void removeFromCatalog(ProductDetails[] products, int password)
				throws RemoteException {
			for (int i = 0; i < products.length; i++)
				map.remove(products[i].id);
		}

		@Override
		public ProductDetails[] searchByDescription(String name)
				throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ProductDetails[] searchById(int id) throws RemoteException {
			ProductDetails[] out = new ProductDetails[1];
			out[0] = map.get(id);
			return out;
		}

		@Override
		public ProductDetails[] searchByName(String name)
				throws RemoteException {
			// TODO Auto-generated method stub
			return null;
		}

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
